package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listTree() {
        // 1. 查出所有分类
        List<CategoryEntity> allCategory = baseMapper.selectList(null);
        // 2. 组装成父子的树形结构
        return allCategory
                .stream()
                // 2.1 找到所有的一级分类
                .filter(categoryEntity -> categoryEntity.getParentCid() == 0)
                .map(menu1 -> {
                    // 拿到一级菜单的子菜单
                    menu1.setChildren(getChildrens(menu1, allCategory));
                    return menu1;
                })
                // 菜单排序
                .sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort())))
                .collect(Collectors.toList());

    }

    @Override
    public void removeMenuByIds(List<Long> asList) {
        // TODO 1. 检查当前删除的菜单，是否被别的地方引用

        // 逻辑删除
        baseMapper.deleteBatchIds(asList);
    }

    @Override
    public List<Long> getCatelogPath(Long catelogId) {
        List<Long> list = new ArrayList<>();
        CategoryEntity categoryEntity = baseMapper.selectById(catelogId);
        list.add(categoryEntity.getCatId());
        while (categoryEntity.getParentCid() != 0) {
            categoryEntity = baseMapper.selectById(categoryEntity.getParentCid());
            list.add(categoryEntity.getCatId());
        }
        Collections.reverse(list);
        return list;
    }

    @Override
    public void updateDetailById(CategoryEntity category) {
        // 修改分类
        this.updateById(category);
        // 修改冗余字段
        categoryBrandRelationService.updateCategoryNameByCatelogId(category.getCatId(), category.getName());
    }

    /**
     * 返回分类 node 的所有子分类
     * @param category 当前分类
     * @param allCategory 所有分类
     * @return 子分类
     */
    private List<CategoryEntity> getChildrens(CategoryEntity category, List<CategoryEntity> allCategory) {
        return allCategory
                .stream()
                // 过滤当前分类的所有子分类
                .filter(menu -> menu.getParentCid().equals(category.getCatId()))
                // 获取每个子分类所有子分类
                .map(menu -> {
                    menu.setChildren(getChildrens(menu, allCategory));
                    return menu;
                })
                // 排序
                .sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort())))
                .collect(Collectors.toList());
    }

}
